package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.gen.entity.HChangeOrder;
import com.zhongwang.mms.gen.entity.HOrder;
import com.zhongwang.mms.module.allocation.dao.AllocationOrderDao;
import com.zhongwang.mms.module.half.dao.HChangeOrderDao;
import com.zhongwang.mms.module.half.dao.HOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description:生成出入库单号
 * @author: th
 * @time: 2020/4/2 13:17
 */
@Service
public class CreateOrderCodeService {

    @Autowired
    private HOrderDao hOrderDao;
    @Autowired
    private HChangeOrderDao hChangeOrderDao;
    @Autowired
    private AllocationOrderDao allocationOrderDao;

    //入库单号
    public String getInOrderCode(){
        // 取得当日零时时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date = calendar.getTime();
        // 查询当日是否有入库记录
        List<HOrder> hOrder = hOrderDao.findAllByDate(date);
        String orderCode; // 入库单编号生产规则：HRK200317001
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        if (hOrder.size() == 0) {
            orderCode = "HRK" + dateFormat.format(new Date()).substring(2,8) + "001";
        } else {
            orderCode = "HRK" + dateFormat.format(new Date()).substring(2,8) + String.format("%03d", Integer.parseInt(hOrder.get(0).getOdrCode().substring(10, 12)) + 1);
        }
        return orderCode;
    }

    //出库单号
    public String getOutOrderCode(){
        // 取得当日零时时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date = calendar.getTime();
        // 查询当日是否有出库记录
        List<HOrder> hOrder = hOrderDao.findAllOutByDate(date);
        String orderCode; // 入库单编号生产规则：HRK200317001
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        if (hOrder.size() == 0) {
            orderCode = "HCK" + dateFormat.format(new Date()).substring(2,8) + "001";
        } else {
            orderCode = "HCK" + dateFormat.format(new Date()).substring(2,8) + String.format("%03d", Integer.parseInt(hOrder.get(0).getOdrCode().substring(10, 12)) + 1);
        }
        return orderCode;
    }

    //串料单号
    public String getChangeOrderCode(){
        // 取得当日零时时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date = calendar.getTime();
        // 查询当日是否有串料记录
        List<HChangeOrder> hOrder = hChangeOrderDao.findAllByDate(date);
        String orderCode; // 入库单编号生产规则：HRK200317001
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        if (hOrder.size() == 0) {
            orderCode = "HCL" + dateFormat.format(new Date()).substring(2,8) + "001";
        } else {
            orderCode = "HCL" + dateFormat.format(new Date()).substring(2,8) + String.format("%03d", Integer.parseInt(hOrder.get(0).getCeoCode().substring(10, 12)) + 1);
        }
        return orderCode;
    }

    /**
     * 调拨单号
     */
    public String getAllocationOrderCode(){
        String newGeneratePRKOrderCode = "";
        //获取最后编码
        String lastPCKOrderCode = allocationOrderDao.findLastMDBOrderCode();
        //获取当前时间简码
        String datetimeCode = new SimpleDateFormat("yyMMdd").format(new Date());
        if (lastPCKOrderCode == null) {
            newGeneratePRKOrderCode = "MDB" + new SimpleDateFormat("yyMMdd").format(new Date()) + "001";
        } else if (!lastPCKOrderCode.substring(3, 9).equals(datetimeCode)) {
            newGeneratePRKOrderCode = "MDB" + new SimpleDateFormat("yyMMdd").format(new Date()) + "001";
        } else {
            //获取外购件库最新最后的入库单号
            Integer sn = Integer.parseInt(lastPCKOrderCode.substring(9));
            newGeneratePRKOrderCode = "MDB" + new SimpleDateFormat("yyMMdd").format(new Date()) + String.format("%03d", (sn + 1)); //25为int型 ;
        }
        return newGeneratePRKOrderCode;
    }
}
